﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectBordspil.Models;
using System.Data.Entity;
using System.Web.Security;
using System.Web.Mvc;


namespace ProjectBordspil.Sevices
{

    public class MemServices
    {
        private IAppRepository _repository = null;

        public MemServices()
        {
            _repository = new AppRepository();
        }

        public MemServices(IAppRepository rep)
        {
            _repository = rep;
        }

        /// <summary>
        /// Set up new memory game
        /// </summary>
        public IEnumerable<MemImageInfo> SetUpGame(int id)
        {
            List<MemImageInfo> gameReady = new List<MemImageInfo>();

            List<MemImages> aListOfImages = new List<MemImages>();

            aListOfImages = _repository.GetAllMemImages;

            for (var i = 0; i < 28; i++)
            {
                MemImageInfo iImage = new MemImageInfo();

                
                iImage.idImage = i + 1;
                iImage.idGameInstance = id;
                iImage.status = "down";
                iImage.userFound = null;
                iImage.theImageObject = aListOfImages[i];

                gameReady.Add(iImage);
            }

            gameReady= gameReady.OrderBy(emp => Guid.NewGuid()).ToList();

            _repository.AddAllMemImageInto(gameReady);

            return gameReady;
        }

        /// <summary>
        /// Creates a new instance of memory game
        /// </summary>
       public ActiveGames CreateNewGame(string name){ 
        
            ActiveGames aNewGame = new ActiveGames();

            Random rnd = new Random();

            int numberOfGame = rnd.Next(1000);

            if (name == null)
            {
                aNewGame.name = "No Name";
            }
            else
            {
                aNewGame.name = name;
            }
            aNewGame.NumberOfPlayers = 1;
            aNewGame.finished = false;
            aNewGame.nameOfView = "MemGameView";
            aNewGame.typeOfGame = "Memory Game";
            aNewGame.user1 = Membership.GetUser().UserName;
            aNewGame.user2 = null;

            _repository.AddNewGame(aNewGame);

            return aNewGame; 
       }

       /// <summary>
       /// Find an instance of a Memory game with some id
       /// </summary>
       public MemViewModel showGameInstace(int id){

            AppDataContext db = new AppDataContext();

            MemViewModel gameIns = new MemViewModel();

            gameIns.theGame = (from game in db.ActiveGames
                            where game.id == id
                            select game).SingleOrDefault();

            gameIns.theImages = (from image in db.MemImageInfo.Include(p => p.theImageObject )
                              where image.idGameInstance == id
                              select image).ToList();

            return gameIns;
        
        }

       /// <summary>
       /// Create a new user
       /// </summary>
       public void CreateNewUser(string userName)
       {

           UserModel newUser = new UserModel();

           newUser.userName = userName;
           newUser.scoreGui = 0;
           newUser.scoreMem = 0;

           _repository.AddUserInfo(newUser);
       }
    }
}